home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 12⁄22⁄89 / SpinAbout.cp < prev    next >
Encoding:
Text File  |  1989-11-20  |  6.6 KB  |  313 lines  |  [TEXT/MPS ]

  1. /*
  2.  * SpinAbout by Andrew Shebanow 10/31/89 (Boo!)
  3.  *
  4.  *    About box XCMD for Spin stack.
  5.  * Displays a picture as a backdrop in a window,
  6.  * and animates a list of names over the top
  7.  */
  8.  
  9. #include <string.h>
  10. #include <stddef.h>
  11.  
  12. #include <Types.h>
  13. #include <Strings.h>
  14. #include <QuickDraw.h>
  15. #include <Fonts.h>
  16. #include <Events.h>
  17. #include <Controls.h>
  18. #include <Windows.h>
  19. #include <Menus.h>
  20. #include <TextEdit.h>
  21. #include <Dialogs.h>
  22. #include <Desk.h>
  23. #include <Scrap.h>
  24. #include <ToolUtils.h>
  25. #include <Memory.h>
  26. #include <Resources.h>
  27. #include <SegLoad.h>
  28. #include <Files.h>
  29. #include <OSUtils.h>
  30. #include <OSEvents.h>
  31. #include <DiskInit.h>
  32. #include <Packages.h>
  33.  
  34. #include <HyperXCMD.h>
  35.  
  36. const short kColorPictID    = 20000;
  37. const short kBWPictID        = 20001;
  38. const short kStringsID        = 20000;
  39.  
  40. class TSpinAbout {
  41. private:
  42.     XCmdPtr        fParamPtr;
  43.     GrafPtr        fSavePort;
  44.     PicHandle    fPicture;
  45.     Rect        fWindRect;
  46.     Rect        fPictRect;
  47.     WindowPtr    fWindow;
  48.     short        fTextVOffset;
  49.     short        fStringIdx;
  50.     long        fLastTicks;
  51.     BitMap*        fScreenBits;
  52.     Str255        fCreditString;
  53.     Boolean        fDone;
  54.     Boolean        fHaveColorQD;
  55.     Boolean        fHaveColorScreen;
  56.  
  57. public:
  58.             TSpinAbout(XCmdPtr paramPtr);
  59.             ~TSpinAbout();
  60.  
  61.     void    DoSpinAbout();
  62.  
  63.     void    GetPicture();
  64.     void    CreateWindow();
  65.     void    GetCreditString();
  66.     void    DrawCredits();
  67.  
  68.     // utility routines
  69.     void    SetReturnString(char* str);
  70.     void    ErrAbort(char* str);
  71.     void    CheckQDEnvironment();
  72.  
  73.     // Do nothing routine to fake out compiler
  74.     void    DoNothing(char* str);
  75. };
  76.  
  77. pascal void EntryPoint(XCmdPtr paramPtr)
  78. {
  79.     TSpinAbout theSpinAbout(paramPtr);
  80.     
  81.     theSpinAbout.DoSpinAbout();
  82. }
  83.  
  84. TSpinAbout::TSpinAbout(XCmdPtr paramPtr)
  85. {
  86.     // get current port - always our card window
  87.     GetPort(&fSavePort);
  88.  
  89.     // save off XCMDPtr for use by other methods
  90.     fParamPtr = paramPtr;
  91.  
  92.     fDone = false;
  93.     fPicture = nil;
  94.     fWindow = nil;
  95.     fStringIdx = 1;
  96.     fLastTicks = 0;
  97.     fCreditString[0] = '\0';
  98.     
  99.     if (fDone)
  100.       DoNothing("SpinAbout by Andrew Shebanow © Copyright 1989 Apple Computer. All Rights Reserved.");
  101.  
  102.     /* Set the cursor to the arrow for use with the dialog box. */
  103.     InitCursor();
  104.     /* Flush all events to be safe. */        
  105.     FlushEvents(everyEvent,0); 
  106.  
  107.     CheckQDEnvironment();
  108. }
  109.  
  110. TSpinAbout::~TSpinAbout()
  111. {
  112.     // clean up and exit
  113.     if (fWindow)
  114.       DisposeWindow(fWindow);
  115.     if (fPicture)
  116.       ReleaseResource((Handle)fPicture);
  117.  
  118.     /* Flush all events to be safe. */        
  119.     FlushEvents(everyEvent,0); 
  120.  
  121.     /*
  122.         Let HyperCard set the cursor to a known state, so the next "idle" message
  123.         after the dialog goes away will reset it to the correct cursor. (HyperCard
  124.         doesn't know we did an InitCursor.)
  125.     */
  126.     SendHCMessage(fParamPtr,"\pset cursor to 4");    /* 4 = the watch cursor */
  127.  
  128.     // restore saved port
  129.     SetPort(fSavePort);
  130. }
  131.  
  132. void TSpinAbout::DoSpinAbout()
  133. {
  134.     // read in the apropos picture
  135.     GetPicture();
  136.     if (fDone) return;            // check for errors
  137.  
  138.     // create the window
  139.     CreateWindow();
  140.     if (fDone) return;            // check for errors
  141.  
  142.     GetCreditString();
  143.     DrawCredits();
  144.     fLastTicks = TickCount();
  145.  
  146.     while (!fDone)
  147.       {
  148.         // get next event
  149.         EventRecord evtRec;
  150.         SystemTask();
  151.         Boolean gotEvent = GetNextEvent(everyEvent,&evtRec);
  152.         
  153.         if (gotEvent)
  154.           switch (evtRec.what)
  155.              {
  156.               case mouseDown:
  157.                   (void) p2cstr(fCreditString);
  158.                   SetReturnString((char*) fCreditString);
  159.                   (void) c2pstr((char*) fCreditString);
  160.                   fDone = true;
  161.                   break;
  162.               case updateEvt:
  163.                   WindowPtr wind = (WindowPtr) evtRec.message;
  164.                   if (wind == fWindow)
  165.                     {
  166.                       SetPort(wind);
  167.                       BeginUpdate(wind);
  168.                       DrawCredits();
  169.                       EndUpdate(wind);
  170.                     }
  171.                   break;
  172.               default:
  173.                   break;
  174.             }
  175.         
  176.         // check if it is time for another string to show up
  177.         long curTicks = TickCount();
  178.         if ((curTicks - fLastTicks) >= 90)
  179.           {
  180.             fLastTicks = curTicks;
  181.             GetCreditString();
  182.             DrawCredits();
  183.           }
  184.       }
  185. }
  186.  
  187. void TSpinAbout::GetPicture()
  188. {
  189.     short pictResID = kBWPictID;
  190.     if (fHaveColorScreen)
  191.       pictResID = kColorPictID;
  192.  
  193.     // get PICT resource
  194.     fPicture = (PicHandle) GetResource('PICT',pictResID);
  195.     if (fPicture == nil)
  196.       {
  197.         if (fHaveColorScreen)
  198.           {
  199.             // couldn't read in mondo color pict, so try for BW
  200.             fPicture = (PicHandle) GetResource('PICT',kBWPictID);
  201.             if (fPicture == nil)
  202.               {
  203.                 ErrAbort("Not enough memory to read picture.");
  204.                 return;
  205.               }
  206.           }
  207.         else
  208.           {
  209.             ErrAbort("Not enough memory to read picture.");
  210.             return;
  211.           }
  212.       }
  213.     HNoPurge((Handle) fPicture);
  214. }
  215.  
  216. void TSpinAbout::CreateWindow()
  217. {
  218.     // get rect of picture
  219.     fWindRect = (*fPicture)->picFrame;
  220.  
  221.     // center it on screen
  222.     OffsetRect(&fWindRect,-fWindRect.left,-fWindRect.top);
  223.     short offH = (fScreenBits->bounds.right - fScreenBits->bounds.left - fWindRect.right) / 2;
  224.     short offV = (fScreenBits->bounds.bottom - fScreenBits->bounds.top - fWindRect.bottom) / 2;
  225.     OffsetRect(&fWindRect,offH,offV);
  226.  
  227.     // create window (in color if available)
  228.     if (fHaveColorQD)
  229.       fWindow = NewCWindow(nil,&fWindRect,"\p",true,dBoxProc,(WindowPtr)-1,false,0);
  230.     else fWindow = NewWindow(nil,&fWindRect,"\p",true,dBoxProc,(WindowPtr)-1,false,0);
  231.     if (fWindow == nil)
  232.       {
  233.         ErrAbort("Not enough memory to allocate window.");
  234.         return;
  235.       }
  236.  
  237.     // make the window the current port
  238.     SetPort(fWindow);
  239.  
  240.     // set up pict rect to be size of window's content region
  241.     fPictRect = fWindow->portRect;
  242.  
  243.     // preprepare font
  244.     TextFont(helvetica);
  245.     TextSize(14);
  246.     TextFace(bold);
  247.  
  248.     // figure out where to draw text (vertically), leave
  249.     // room for text above centerline
  250.     fTextVOffset = ((fPictRect.bottom - fPictRect.top) / 2) + 20;
  251. }
  252.  
  253. void TSpinAbout::GetCreditString()
  254. {
  255.     GetIndString(fCreditString,kStringsID,fStringIdx);
  256.     if (Length(fCreditString) == 0)
  257.       fStringIdx = 1;    // set up to start all over
  258.      else fStringIdx++;
  259. }
  260.  
  261. void TSpinAbout::DrawCredits()
  262. {
  263.     DrawPicture(fPicture,&fPictRect);
  264.  
  265.     short tWidth = StringWidth(fCreditString);
  266.     short dh = (fPictRect.right - fPictRect.left - tWidth) / 2;
  267.     MoveTo(fPictRect.left + dh, fTextVOffset);
  268.     DrawString(fCreditString);
  269. }
  270.  
  271. void TSpinAbout::SetReturnString(char *str)
  272. {
  273.     Handle  nuHndl;
  274.  
  275.     nuHndl = NewHandle((long)(strlen(str)+1));
  276.     if (nuHndl == nil) return;
  277.     strcpy((char *)*nuHndl,str);
  278.     fParamPtr->returnValue = nuHndl;
  279. }
  280.  
  281. void TSpinAbout::ErrAbort(char *str)
  282. {
  283.     SetReturnString(str);
  284.     fDone = true;
  285. }
  286.  
  287. void TSpinAbout::CheckQDEnvironment()
  288. {
  289.     SysEnvRec env;
  290.  
  291.     fHaveColorScreen = false;
  292.  
  293.     // do we have color QD? (ask SysEnvirons)
  294.     (void) SysEnvirons(curSysEnvVers,&env);
  295.     fHaveColorQD = env.hasColorQD;
  296.  
  297.     // do we have a color screen to draw on?
  298.     if (fHaveColorQD)
  299.       {
  300.         GDHandle gd = GetMainDevice();
  301.         if ((*(*gd)->gdPMap)->pixelSize > 1)
  302.           fHaveColorScreen = true;
  303.       }
  304.  
  305.     // get coordinates of screen
  306.     long* thePortPtr = (long*) SetCurrentA5();
  307.     fScreenBits = (BitMap*) (*thePortPtr - 122);
  308. }
  309.  
  310. void TSpinAbout::DoNothing(char* str)
  311. {
  312. }
  313.